package com.situ.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.web.entity.User;

public class LogoutServlet extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			//销毁Session
			session.invalidate();
			
			ServletContext servletContext = getServletContext();
			List<User> list = (List<User>) servletContext.getAttribute("onLineUserList");
			list.remove(user);
			servletContext.setAttribute("onLineUserList", list);
			
			//重定向到登录界面
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
		}
	}
}
